<?
class graphic 
{
	var $cIdGrafico = "grafico";
	var $nType = GRAPHIC_BAR;
	var $nWidth = 400;
	var $nHeight = 400;
	var $aColor = array ( "red", "blue", "yellow", "green");
	var $aBorderColor = array ( "red", "blue", "yellow", "green");
	var $aSeries;
	//  Propiedades de las Barras
	var $cColorRegla = "#000000";
	var $cColorLinea = "#dedede";
	var $nProporcionBarra = 0.3;
	var $cDatoComparativo ;
	var $nqReglaY  = 10;
	
	function graphic  ( $cIdGrafico = "grafico", $nType = 1, $nWidth = 400, $nHeight = 400) 
	{
		$this->cIdGrafico = $cIdGrafico;
		$this->nType = $nType;
		$this->nWidth = $nWidth;
		$this->nHeight = $nHeight;
	}
	function setColorLinea ( $cColor ) 
	{
		$this->cColorLinea  = $cColor ;
	}
	function setColorRegla ( $cColor ) 
	{
		$this->cColorRegla  = $cColor ;
	}
	function setProportion ( $nProp ) 
	{
		$this->nProporcionBarra  = $nProp ;
	}
	function setWidth ( $nWidth ) 
	{
		$this->nWidth  = $nWidth ;
	}
	function setHeight( $nHeight ) 
	{
		$this->nHeight  = $nHeight ;
	}
	
	function make ()
	{
		switch( $this->nType )
		{
			case GRAPHIC_BAR: return $this->makeBar(); break;
		}
	}
	function addSerie( $cName, $aSerie )
	{
		$this->aSeries[$cName] = $aSerie; 
	}
	function setColor( $aColor )
	{
		$this->aColor= $aColor; 
	}
	function setBorderColor( $aColor )
	{
		$this->aBorderColor= $aColor; 
	}	
	function setGraphicType ( $nType )
	{
		$this->nType = $nType;
	}
	function setDatoComparativo( $cDatoComparativo )
	{
		$this->cDatoComparativo = $cDatoComparativo;
	}
	
	function makeBar()
	{
		$nTitulo = 10;
		$nReglaY = 60;
		$nReglaX = 20;
		
		$nqItems = 0;
		$nMaxValor = 0;
		
		// Busca el maximo valor
		foreach(  $this->aSeries as $cName =>  $aSerie )
		{
			foreach( $aSerie as $cKey =>  $nValue )	
			{
				if ( $nValue > $nMaxValor ) $nMaxValor = $nValue;
				$nqItems++;
			}
		}
		// Calcula coeficientes
		$nMaxHeight = pow(10 , strlen($nMaxValor) );
		if ( $nMaxHeight / 2 > $nMaxValor )  $nMaxHeight = $nMaxHeight/ 2;
		if ( $nMaxHeight / 2 > $nMaxValor )  $nMaxHeight = $nMaxHeight/ 2;
		$nPxH = ( $this->nHeight - $nReglaX - $nTitulo) / $nMaxHeight;
		$nPxW = ( $this->nWidth - $nReglaY )  / $nqItems;
		$nWidthBarra = $nPxW * $this->nProporcionBarra ;
		$nSeparador = $nPxW * (1 -  $this->nProporcionBarra )  / 2;
		$cReturn .= "<div id='" . $this->cIdGrafico. "' style=' position: relative; width: " . $this->nWidth. "px; height: " . $this->nHeight .  "px;' >";
		// Arma la regla Y
		$cReturn .= "<div style='position: absolute; left: " . $nReglaY . "px; top: " . $nTitulo. "px; width: 1px; height: " . ( $this->nHeight - $nTitulo - $nReglaX) . "px; border-left: " . $this->cColorRegla . " solid 1px' ></div>";
		$nSalto = $nMaxHeight/ $this->nqReglaY;
		for ( $nY = $nSalto; $nY <= $nMaxHeight ; $nY += $nSalto )
		{
			//echo "( ($this->nHeight - $nReglaX  - $nTitulo)  / $this->nqReglaY )";
			$nTop = $nPxH * ( $nMaxHeight - $nY );
			// Texto Eje Y
			$cReturn .= "<div style='text-align: right; position: absolute; left: 0px; top: " . ($nTop + $nTitulo - 6 ). "px; width: " . ($nReglaY - 6) . "px; height: " . ( ( $this->nHeight - $nReglaX  - $nTitulo)  / $this->nqReglaY ) . "px;' class='ejeY' >" . number_format($nY,'','','.') . "</div>";
			// Lineas continua del eje Y
			$cReturn .= "<div style='position: absolute; left: " . ($nReglaY ). "px; top: " . ($nTop + $nTitulo ). "px; width: ". ( $this->nWidth - $nReglaY ) . "px; height: 1px; border-top: " . $this->cColorLinea . " solid 1px' ></div>";
			// Lineas de los valores del  Eje Y
			$cReturn .= "<div style='position: absolute; left: " . ($nReglaY - 3). "px; top: " . ($nTop + $nTitulo ). "px; width: 6px; height: 1px; border-top: " . $this->cColorRegla . " solid 1px' ></div>";
		}
		// Arma la regla X
		$cReturn .= "<div style='z-index: 99; position: absolute; left: 0px; top: " . ( $this->nHeight - $nReglaX ) . "px; height: 1px; width: " . ( $this->nWidth ) . "px; border-top: " . $this->cColorRegla . " solid 1px' ></div>";
		// Arma las Barras
		$nPosX = $nReglaY  + $nSeparador;
		$nqItems = 0;
		foreach(  $this->aSeries as $cName =>  $aSerie )
		{
			foreach( $aSerie as $cKey =>  $nValue )	
			{
				$cValue = number_format($nValue,'','','.');
				$nTopBarra  = floor($nPxH * ( $nMaxHeight - $nValue) ) + $nTitulo;
				$nHeightBarra  = floor($nPxH * $nValue);
				$cColor = $this->aColor[$nqItems++ % count($this->aColor) ];
				$cBorderColor = $this->aBorderColor[$nqItems % count($this->aBorderColor) ];
				// Incremento
				if ( $this->cDatoComparativo && $cKey != $this->cDatoComparativo )
				{
					$cTexto = "Incremento $cKey / " . $this->cDatoComparativo . " " . round((($aSerie[$this->cDatoComparativo] / $nValue) - 1 )* 100, 1)  . "%";
					$cReturn .= "<div style='text-align:center; position: absolute; left: " . ( $nReglaY  + ($nqItems) * $nPxW - ($nPxW/4) ) . "px; top: " . (($this->nHeight - $nReglaX - $nTitulo) /2)  . "px;  width: " . ($nSeparador * 1.5)  . "px; ' class='incremento' >$cTexto</div>";
				}
				// Barra
				$cReturn .= "<div style='text-align:center; position: absolute; left: " . $nPosX . "px; top: " . $nTopBarra . "px; height: " . $nHeightBarra ."px; width: " . $nWidthBarra . "px; background-color: $cColor;  border: $cBorderColor  solid 1px' ></div>";
				// Valor
				$cReturn .= "<div style='text-align:center; position: absolute; left: " . $nPosX . "px; top: " . $nTopBarra . "px; width: " . $nWidthBarra . "px;' class='valores' >$cValue</div>";
				// Key EjeX
				if ( $cKey )  $cReturn .= "<div style='text-align:center; position: absolute; left: " . ($nPosX - $nSeparador). "px; top: " . ( $this->nHeight - $nReglaX ) . "px; height: " . $nReglaX ."px; width: " . $nPxW . "px;' class='ejeX' >$cKey</div>";
				// Linea del Eje X
				$cReturn .= "<div style='position: absolute; left: " . ($nReglaY  + ($nqItems-1) * $nPxW ). "px; top: " . ( $this->nHeight - $nReglaX - 10 )  .  "px; width: 1px; height: 20px; border-left: " . $this->cColorRegla . " solid 1px' ></div>";
				$nPosX += $nPxW;
			}
		}
		
		$cReturn .= "</div>";
		return $cReturn ;
	}
}
?>